
    /**  
    * @Title: CreateServiceClass.java
    * @Package com.utils.table
    * @Description: 
    * @author likai
    * @date 2017年4月16日
    * @version V1.0  
    */
    
package com.frames.table;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.utils.bean.BeanUtil;
import com.utils.date.DateUtil;
import com.utils.properties.PropertyUtil;

/**
    * @ClassName: CreateServiceClass
    * @Description: 创建项目的表的业务层，自动生成业务相关的controller、service、serviceParam,以及相关增删改查接口和实现mappper
    * @author likai
    * @date 2017年4月16日
    *
    */
@Deprecated
public class CreateTableService{
	public static Logger log = LoggerFactory.getLogger(CreateTableService.class);
	private static  Properties prop;
	public CreateTableService(String tableProp) {
		super();
		prop=PropertyUtil.getProp(tableProp);
	}
	public String getProperty(String key) {
		String encode="utf-8";
		try {
			return new String(prop.getProperty(key).getBytes("ISO8859-1"), encode);
		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}
	
	public void createServiceClass(String servicePrefix,String describe) {
		
		//String mapperSuffix= BeanUtil.columnToField2(getProperty("mapperSuffix").toLowerCase());
		servicePrefix = BeanUtil.columnToField2(servicePrefix);
		String controllerPath=getProperty("controllerPath").replace(".", "/");
		String controllerPackage=getProperty("controllerPackage").replace("/", ".");
		
		String servicePath=getProperty("servicePath").replace(".", "/");
		String servicePackage=getProperty("servicePackage").replace("/", ".");
		
		String serviceImplPath=getProperty("serviceImplPath").replace(".", "/");
		String serviceImplPackage=getProperty("serviceImplPackage").replace("/", ".");
		
		//String serviceParamPath=getProperty("serviceParamPath").replace(".", "/");
		//String serviceParamPackage=getProperty("serviceParamPackage").replace("/", ".");
		
/*		String mapperServicePath=getProperty("mapperServicePath").replace(".", "/");
		String mapperServicPackage=getProperty("mapperServicPackage").replace("/", ".");
		
		String mapperXmlServicPath=getProperty("mapperXmlServicPath").replace(".", "/");
		String mapperXmlServicPackage=getProperty("mapperXmlServicPackage").replace("/", ".");*/
		
		if (!controllerPath.equals("")&&!controllerPackage.equals("")) {
			//创建controller
			String tmpClassName =controllerPath+"/"+ servicePrefix + "Controller.java";
			File classFile = new File(tmpClassName);
			if (!classFile.exists()) {// 如果存在，则不重建
				createController(servicePrefix,controllerPackage,servicePackage,describe,classFile);
			}
		}
		if (!servicePath.equals("")&&!servicePackage.equals("")) {
			//创建service接口
			String tmpClassName =servicePath+"/"+ servicePrefix + "Service.java";
			File classFile = new File(tmpClassName);
			if (!classFile.exists()) {// 如果存在，则不重建
				createService(servicePrefix, servicePackage, describe, classFile);
			}
		}
		if (!serviceImplPath.equals("")&&!serviceImplPackage.equals("")) {
			//创建service接口实现类
			String tmpClassName =serviceImplPath+"/"+ servicePrefix + "ServiceImpl.java";
			File classFile = new File(tmpClassName);
			if (!classFile.exists()) {// 如果存在，则不重建
				createServiceImpl(servicePrefix, serviceImplPackage, servicePackage, describe, classFile);
			}
			
		}
		/*if (!serviceParamPath.equals("")&&!serviceParamPackage.equals("")) {
			//创建service参数加工类
			String tmpClassName =serviceParamPath+"/"+ servicePrefix + "ServiceParam.java";
			File classFile = new File(tmpClassName);
			if (!classFile.exists()) {// 如果存在，则不重建
				createServiceParam(servicePrefix, serviceParamPackage,mapperServicPackage, describe, classFile);
			}
			
		}*/
		/*if (!mapperServicePath.equals("")&&!mapperServicPackage.equals("")) {
			//创建service业务数据库接口
			String tmpClassName =mapperServicePath+"/"+ servicePrefix+mapperSuffix + ".java";
			File classFile = new File(tmpClassName);
			if (!classFile.exists()) {// 如果存在，则不重建
				createMapper(servicePrefix, mapperServicPackage,mapperSuffix, describe, classFile);
			}
			
		}
		if (!mapperXmlServicPath.equals("")&&!mapperXmlServicPackage.equals("")) {
			//创建service业务数据库接口实现mapper
			String tmpClassName =mapperXmlServicPath+"/"+ servicePrefix+mapperSuffix + "Mapper.xml";
			File classFile = new File(tmpClassName);
			if (!classFile.exists()) {// 如果存在，则不重建
				createMapperXml(mapperServicPackage+"."+servicePrefix+mapperSuffix,classFile);
			}
			
		}*/
		log.info("------创建成功-----");
	}
	
	private void createController(String className,String classPackage,String servicePackage,String describe,File classFile) {
		try {
			classFile.createNewFile();
			StringBuilder sb = new StringBuilder();
			sb.append("package " + classPackage + ";\n\n");
			
			sb.append("import org.springframework.stereotype.Controller;\n");
			sb.append("import javax.annotation.Resource;\n");
			sb.append("import org.springframework.web.bind.annotation.RequestMapping;\n");
			sb.append("import "+servicePackage+"."+className+"Service;\n");
			sb.append("\n\n");
			sb.append("/**\n	* @ClassName: "+className+"Controller\n	* @Description: "+describe+"\n	* @author likai\n	* @date "+DateUtil.CHINESE.getDateText(new Date())+"\n	*\n	*/\n\n");
			sb.append("@Controller\n");
			sb.append("@RequestMapping(\"/"+BeanUtil.lowerCaseFirst(className)+"\")\n");
			sb.append("public class " + className + "Controller extends SuperApi{\n\n");
			//
			sb.append("	@Resource\n");
			sb.append("	public "+className+"Service "+BeanUtil.lowerCaseFirst(className)+"ServiceImpl;\n");
			sb.append("\n\n}\n\n");
			OutputStream os = new FileOutputStream(classFile);
			PrintStream ps = new PrintStream(os);
			ps.print(sb);

			os.flush();
			os.close();
			ps.flush();
			ps.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void createService(String className,String classPackage,String describe,File classFile) {
		try {
			classFile.createNewFile();
			StringBuilder sb = new StringBuilder();
			sb.append("package " + classPackage + ";\n\n");
			
			sb.append("/**\n	* @ClassName: "+className+"Service\n	* @Description: "+describe+"\n	* @author likai\n	* @date "+DateUtil.CHINESE.getDateText(new Date())+"\n	*\n	*/\n\n");
			sb.append("public interface " + className + "Service{\n\n");
			sb.append("\n}\n\n");
			OutputStream os = new FileOutputStream(classFile);
			PrintStream ps = new PrintStream(os);
			ps.print(sb);

			os.flush();
			os.close();
			ps.flush();
			ps.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void createServiceImpl(String className,String classPackage,String servicePackage,String describe,File classFile) {
		try {
			classFile.createNewFile();
		//	String mapperSuffix= BeanUtil.columnToField2(getProperty("mapperSuffix").toLowerCase());
			StringBuilder sb = new StringBuilder();
			sb.append("package " + classPackage + ";\n\n");
			
			sb.append("import org.springframework.stereotype.Service;\n");
			sb.append("import org.springframework.transaction.annotation.Transactional;\n");
			//sb.append("import javax.annotation.Resource;\n");
			sb.append("import "+servicePackage+"."+className+"Service;\n");
/*			sb.append("import "+serviceParamPackage+".TableParamDeal;\n");
			sb.append("import "+serviceParamPackage+".BussinessParamDeal;\n");*/
			sb.append("\n\n");
			sb.append("/**\n	* @ClassName: "+className+"ServiceImpl\n	* @Description: "+describe+"\n	* @author likai\n	* @date "+DateUtil.CHINESE.getDateText(new Date())+"\n	*\n	*/\n\n");
			sb.append("@Service\n");
			sb.append("@Transactional\n");
			sb.append("public class " + className + "ServiceImpl extends SuperService implements "+className+"Service{\n\n");
			//
/*			sb.append("	@Resource\n");
			sb.append("	public TableParamDeal tableParamDeal;\n");
			sb.append("	@Resource\n");
			sb.append("	public BussinessParamDeal bussinessParamDeal;\n");*/
			sb.append("\n\n}\n\n");
			OutputStream os = new FileOutputStream(classFile);
			PrintStream ps = new PrintStream(os);
			ps.print(sb);

			os.flush();
			os.close();
			ps.flush();
			ps.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
//	private void createServiceParam(String className,String classPackage,String mapperServicPackage,String describe,File classFile) {
//		try {
//			String mapperSuffix= BeanUtil.columnToField2(getProperty("mapperSuffix").toLowerCase());
//			classFile.createNewFile();
//			StringBuilder sb = new StringBuilder();
//			sb.append("package " + classPackage + ";\n\n");
//			sb.append("import javax.annotation.Resource;\n");
//			sb.append("import com.supers.pojo.LogUtil;\n");
//			sb.append("import org.springframework.stereotype.Component;\n");
//			sb.append("import "+mapperServicPackage+"."+className+mapperSuffix+";\n");
//			sb.append("\n\n");
//			sb.append("/**\n	* @ClassName: "+className+"ServiceParam\n	* @Description: "+describe+"\n	* @author likai\n	* @date "+DateUtil.CHINESE.getDateText(new Date())+"\n	*\n	*/\n\n");
//			sb.append("@Component(value=\""+BeanUtil.lowerCaseFirst(className)+"ServiceParam\")\n");
//			sb.append("public class " + className + "ServiceParam extends LogUtil{\n\n");
//			sb.append("	@Resource\n");
//			sb.append("	public "+className+mapperSuffix+" "+BeanUtil.lowerCaseFirst(className)+mapperSuffix+";\n");
//			sb.append("\n\n}\n\n");
//			OutputStream os = new FileOutputStream(classFile);
//			PrintStream ps = new PrintStream(os);
//			ps.print(sb);
//
//			os.flush();
//			os.close();
//			ps.flush();
//			ps.close();
//
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//	}
	
	public void createMapper(String className,String classPackage,String mapperSuffix,String describe,File classFile) {
		try {
			classFile.createNewFile();
			StringBuilder sb = new StringBuilder();
			sb.append("package " + classPackage + ";\n\n");
			
			sb.append("/**\n	* @ClassName: "+className+mapperSuffix+"\n	* @Description: "+describe+"\n	* @author likai\n	* @date "+DateUtil.CHINESE.getDateText(new Date())+"\n	*\n	*/\n\n");
			sb.append("public interface " + className+mapperSuffix + "{\n\n");
			sb.append("\n}\n\n");
			OutputStream os = new FileOutputStream(classFile);
			PrintStream ps = new PrintStream(os);
			ps.print(sb);

			os.flush();
			os.close();
			ps.flush();
			ps.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public  void createMapperXml(String mapperServicClass,File classFile) {
		try {
			classFile.createNewFile();
			StringBuilder sb = new StringBuilder();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			sb.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n");
			sb.append("<mapper namespace=\""+mapperServicClass+"\" >\n\n\n\n");									
			sb.append("</mapper>");
			OutputStream os = new FileOutputStream(classFile);
			PrintStream ps = new PrintStream(os);
			ps.print(sb);
			os.flush();
			os.close();
			ps.flush();
			ps.close();
		}catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	
}
